package com.cetool.autocms.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cetool.autocms.common.PageRequest;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.exception.ServiceException;
import com.cetool.autocms.mapper.user.ClientUserMapper;
import com.cetool.autocms.pojo.domain.user.ClientUserInfoDO;
import com.cetool.autocms.pojo.dto.user.ClientUserInfoDTO;
import com.cetool.autocms.pojo.param.user.QueryClientUserListParam;
import com.cetool.autocms.pojo.param.user.SaveClientUserParam;
import com.cetool.autocms.pojo.param.user.UpdateClientUserParam;
import com.cetool.autocms.pojo.request.user.DeleteUserRequest;
import com.cetool.autocms.pojo.request.user.QueryClientUserListRequest;
import com.cetool.autocms.pojo.request.user.SaveClientUserRequest;
import com.cetool.autocms.pojo.request.user.UpdateClientUserRequest;
import com.cetool.autocms.service.enumeration.UserExceptionEnum;
import com.cetool.autocms.service.user.ClientUserService;
import com.cetool.autocms.util.BeansUtil;
import com.github.pagehelper.PageSerializable;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * 客户端用户信息服务实现
 *
 * @author 戴越
 * @date 2021/11/16
 */
@Service
@Slf4j
public class ClientUserServiceImpl implements ClientUserService {
    /**
     * 注入客户端用户管理持久层
     */
    @Autowired
    private ClientUserMapper clientUserMapper;

    @Override
    public OperateEnum saveClientUser(SaveClientUserRequest request) {
        SaveClientUserParam param = new SaveClientUserParam();
        BeanUtil.copyProperties(request, param);
        param.setUserPassWord(DigestUtils.md5DigestAsHex((param.getUserName() + param.getUserPassWord()).getBytes()).toUpperCase());
        if (clientUserMapper.queryClientUserIsExist(param) > 0) {
            throw new ServiceException(UserExceptionEnum.CREATE_USER_INFO_FAILED);
        }
        //创建客户端用户
        clientUserMapper.saveClientUser(param);
        return OperateEnum.ADD_SUCCESS;
    }

    @Override
    public PageSerializable<ClientUserInfoDTO> listPageClientUser(PageRequest<QueryClientUserListRequest> request) {
        QueryClientUserListParam param = new QueryClientUserListParam();
        BeanUtil.copyProperties(request.getData(), param);
        PageMethod.startPage(request.getPageNum(), request.getPageSize());
        return BeansUtil.pageCopy(new PageSerializable<>(clientUserMapper.listClientUser(param)), ClientUserInfoDTO.class);
    }

    @Override
    public OperateEnum deleteClientUser(DeleteUserRequest request) {
        Integer userId = request.getUserId();
        //删除客户端用户
        //查询删除客户端用户是否存在
        ClientUserInfoDO clientUser = clientUserMapper.getClientUser(userId);
        if (ObjectUtil.isNull(clientUser)) {
            throw new ServiceException(UserExceptionEnum.CHECK_USER_NO_EXIST);
        }
        clientUserMapper.deleteClientUser(userId);
        return OperateEnum.DELETE_SUCCESS;
    }

    @Override
    public OperateEnum updateClientUser(UpdateClientUserRequest request) {
        UpdateClientUserParam param = new UpdateClientUserParam();
        BeanUtil.copyProperties(request, param);
        param.setUserPassWord(DigestUtils.md5DigestAsHex((param.getUserName() + param.getUserPassWord()).getBytes()).toUpperCase());
        clientUserMapper.updateClientUser(param);
        return OperateEnum.UPDATE_SUCCESS;
    }

}
